package com.spa.modules.spa.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.common.api.vo.Result;
import com.spa.common.utils.RedisUtil;
import com.spa.modules.spa.entity.SpaMenu;
import com.spa.modules.spa.mapper.SpaMenuMapper;
import com.spa.modules.spa.vo.SpaMenuMetaVO;
import com.spa.modules.spa.vo.SpaMenuTrueVO;
import com.spa.modules.spa.vo.SpaMenuVO;
import com.spa.modules.system.entity.SpaUser;

import cn.dev33.satoken.stp.StpUtil;

/**
 * 描述: 菜单
 * author: zw
 * date: 2023-01-14 14:23:46
 */
@Service
public class SpaMenuService extends ServiceImpl<SpaMenuMapper, SpaMenu> {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SpaMenuMapper spaMenuMapper;

    /**
     * 分页列表查询
     *
     * @param spaMenu
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Result<Object> queryPageList(HttpServletRequest req, Integer pageNo, Integer pageSize) {
        Page<SpaMenu> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpaMenu> queryWrapper = new LambdaQueryWrapper<>();
        Page<SpaMenu> articlePage = spaMenuMapper.selectPage(page, queryWrapper);
        List<SpaMenu> records = articlePage.getRecords();
        return Result.resultOK(records);
    }
    
    /**
     * 根据用户角色获取菜单树
     * 
     * @return
     */
    public Result<Object> getMenuTrueAll() {
        List<SpaMenu> spaMenuList = spaMenuMapper.selectList(null);
        List<SpaMenuTrueVO> spaMenuTrueVOList = setMenuTrueVO(spaMenuList, "0");
        SpaMenuTrueVO spaMenuTrueVO = new SpaMenuTrueVO();
        spaMenuTrueVO.setId("0");
        spaMenuTrueVO.setTitle("无");
        spaMenuTrueVOList.add(spaMenuTrueVO);
        return Result.resultOK(spaMenuTrueVOList);
    }

    /**
     * 根据用户角色获取菜单树
     * 
     * @return
     */
    public Result<Object> getMenuTrue() {
        List<SpaMenu> spaMenuList = spaMenuMapper.selectList(null);

        List<SpaMenuTrueVO> spaMenuTrueVOList = setMenuTrueVO(spaMenuList, "0");
        return Result.resultOK(spaMenuTrueVOList);
    }

    /**
     * 组装返回结果集
     * 
     * @param spaMenuList
     * @param parentId
     * @return
     */
    private List<SpaMenuTrueVO> setMenuTrueVO(List<SpaMenu> spaMenuList, String parentId) {
        List<SpaMenuTrueVO> spaMenuTrueVOList = new ArrayList<SpaMenuTrueVO>();
        for (SpaMenu spaMenu : spaMenuList) {
            if (parentId.equals(spaMenu.getParentId())) {
                SpaMenuTrueVO spaMenuTrueVO = new SpaMenuTrueVO();

                List<SpaMenuTrueVO> childrenList = setMenuTrueVO(spaMenuList, spaMenu.getId().toString());
                if (childrenList.size() > 0) {
                    spaMenuTrueVO.setChildren(childrenList);
                }

                spaMenuTrueVO.setId(spaMenu.getId().toString());
                spaMenuTrueVO.setParentId(spaMenu.getParentId());
                spaMenuTrueVO.setPath(spaMenu.getPath());
                spaMenuTrueVO.setComponent(spaMenu.getComponent());
                spaMenuTrueVO.setOrderNo(spaMenu.getOrderNo());
                spaMenuTrueVO.setIcon(spaMenu.getIcon());
                spaMenuTrueVO.setTitle(spaMenu.getTitle());
                spaMenuTrueVO.setPermission(spaMenu.getPermission());
                spaMenuTrueVOList.add(spaMenuTrueVO);
            }
        }
        return spaMenuTrueVOList;
    }

    /**
     * 根据登录用户角色获取菜单
     * 
     * @return
     */
    public Result<Object> getMenuListByLoginId() {
        String userId = (String) StpUtil.getLoginId();
        String bayUserJson = (String) redisUtil.get("userInfo:" + userId);
        
        SpaUser bayUser = JSONObject.parseObject(bayUserJson, SpaUser.class);
        bayUser.getRoleId();
        String roleMenuJson = (String) redisUtil.get("roleMenu:"+bayUser.getRoleId());
        
        List<SpaMenu> spaMenuList = JSON.parseArray(roleMenuJson, SpaMenu.class);
        List<SpaMenuVO> spaMenuVOList = setSpaMenuVO(spaMenuList, "0");
        return Result.resultOK(spaMenuVOList);
    }

    /**
     * 根据登录用户角色获取菜单
     * 
     * @return
     */
    public Result<Object> getMenuListByRole() {
        List<SpaMenu> spaMenuList = spaMenuMapper.selectList(null);

        List<SpaMenuVO> spaMenuVOList = setSpaMenuVO(spaMenuList, "0");
        return Result.resultOK(spaMenuVOList);
    }

    /**
     * 组装返回结果集
     * 
     * @param spaMenuList
     * @param parentId
     * @return
     */
    private List<SpaMenuVO> setSpaMenuVO(List<SpaMenu> spaMenuList, String parentId) {
        List<SpaMenuVO> spaMenuVOList = new ArrayList<SpaMenuVO>();
        for (SpaMenu spaMenu : spaMenuList) {
            if (parentId.equals(spaMenu.getParentId())) {
                SpaMenuVO spaMenuVO = new SpaMenuVO();

                List<SpaMenuVO> childrenList = setSpaMenuVO(spaMenuList, spaMenu.getId().toString());
                if (childrenList.size() > 0) {
                    spaMenuVO.setChildren(childrenList);
                }
                spaMenuVO.setComponent(spaMenu.getComponent());
                SpaMenuMetaVO spaMenuMetaVO = new SpaMenuMetaVO();
                spaMenuMetaVO.setOrderNo(spaMenu.getOrderNo());
                spaMenuMetaVO.setTitle(spaMenu.getTitle());
                spaMenuVO.setMeta(spaMenuMetaVO);
                spaMenuVO.setPath(spaMenu.getPath());
                spaMenuVOList.add(spaMenuVO);
            }
        }
        return spaMenuVOList;
    }

    /**
     * 添加
     *
     * @param testTabel
     * @return
     */
    @Transactional
    public Result<Object> insert(SpaMenu spaMenu) {
        spaMenuMapper.insert(spaMenu);
        return Result.resultOK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param spaMenu
     * @return
     */
    @Transactional
    public Result<Object> saveById(SpaMenu spaMenu) {
        spaMenuMapper.updateById(spaMenu);
        return Result.resultOK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @Transactional
    public Result<Object> deleteById(Long id) {
        spaMenuMapper.deleteById(id);
        return Result.resultOK("删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    public Result<Object> selectById(Long id) {
        SpaMenu spaMenu = spaMenuMapper.selectById(id);
        return Result.resultOK(spaMenu);
    }
}